using System;
using System.Collections.Generic;
using UnityEngine;

namespace HCore.Tests.Runtime.BlurEffect.Scripts
{
    public class BaseController : MonoBehaviour
    {
        public UINodeData[] UIListData;
        private readonly Dictionary<string, UINodeData> mViewDic = new();
        private Transform mTempParentNode;

        private void Awake()
        {
            foreach (var uiNodeData in UIListData) mViewDic.Add(uiNodeData.Prefab.name, uiNodeData);

            InitEvents();
        }

        private void OnDestroy()
        {
            RemoveAllEvent();
        }

        protected void OpenView(string viewName, out GameObject targetGo)
        {
            Debug.Log(viewName);
            if (mViewDic.ContainsKey(viewName))
            {
                mTempParentNode = PanelMgr.Instance.GetBaseViewParentNode(mViewDic[viewName].UILayer);
                targetGo = Instantiate(mViewDic[viewName].Prefab, mTempParentNode);
                targetGo.GetComponent<RectTransform>().anchoredPosition = Vector3.zero;
            }
            else
            {
                targetGo = null;
            }
        }

        protected virtual void InitEvents()
        {
        }

        protected virtual void RemoveAllEvent()
        {
        }
    }

    [Serializable]
    public class UINodeData
    {
        public GameObject Prefab;
        public UILayer UILayer;
    }
}